package com.petsdar.controller;

import com.petsdar.service.AccountService;
import com.petsdar.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/6/27
 * Time: 18:54
 * Description: 登录账户controller层
 * Version: V1.0
 */

@RestController
public class AccountController {
    /** account的业务逻辑处理对象 */
    private AccountService accountService;

    @Autowired
    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }


    /**
     * 登录请求，返回json响应结果
     * @param accountJson 前端发送的用户登录的json数据
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestBody String accountJson){
        HashMap<String, String> data = accountService.login(accountJson);
        if (!data.isEmpty()){
            return ResultUtil.success(data);
        }
        return ResultUtil.fail();
    }


    /**
     * 退出登录请求，返回json响应结果
     * @param outJson 前端发送的用户退出登录的json数据
     * @return
     */
    @PostMapping("/loginout")
    public String loginOut(@RequestBody String outJson){
        if (accountService.loginOut(outJson)){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 账户重置密码
     * @param userJson
     * @return
     */
    @PostMapping("/resetpwd")
    public String resetPwd(@RequestBody String userJson){
        boolean result = accountService.resetPwd(userJson);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

}
